Turns the order of the items in target around, in place: the first item becomes the last and the last becomes the first. It changes target itself and makes no copy. Any kind of item can be reversed, text included.
list_reverse(target)| Name | Type |
|---|---|
target | list |
Nothing.
Turns the order of the items in target, a list, around. The first item becomes the last, the last becomes the first, and the ones in between swap the same way, all the way to the middle.
list_reverse(scores); // [10, 20, 30] becomes [30, 20, 10]It is the quick way to get a list the other way round: a high-score table that was filled smallest first, a trail of moves you want to walk back along, or a list_sort that came out the wrong way.
list_reverse moves the items that are already in target. It does not make a new list, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with list_copy first and reverse the one you want turned around.
It turns around the items the list holds right now, the number list_length gives you. The empty places up to the capacity are not touched: a list with room for 8 that holds 3 items still holds 3, at the front, afterwards.
Any kind of item can be reversed: numbers, text, structs, references. Nothing is compared, so there is no rule about what the list holds.
| You do this | What happens |
|---|---|
| reverse a list with no items or one item | nothing changes |
| reverse a list that has the same item more than once | works as normal. Equal items are interchangeable, so you cannot tell which is which |
| reverse it twice | you are back where you started |
| give it an array | refused when checked. An array has its own, array_reverse |
| use it where a value is needed | refused when checked. It gives nothing back |
A script that takes a list as a parameter works on the caller's own list (see Lists). So list_reverse inside a script reverses the caller's list.
Nothing is set aside: the items are swapped where they stand, one swap for every two items. No timing was measured.
/// @type {list<string>}
/// @capacity 4
var deck = ["ace", "king", "queen"];
list_reverse(deck); // deck is now "queen", "king", "ace"
var top = deck[0]; // "queen"Reverses the list_length(target) items the list holds - never its capacity, so a list with room to spare keeps its empty room at the end. Reverse (the GBA module's own alias for std::reverse): it swaps the first item with the last, the second with the second-last, and so on towards the middle, in place - no copy and no extra memory, O(n) in how many items it turns around. Nothing is compared, so any kind of item works, text and structs included. With no items or one item there is nothing to swap and nothing happens.